﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Condimar.Entities;
using Condimar.BussinesLogic;

namespace Condimar.UI
{
    public partial class FrmBuscarCliente : Form
    {
        [Dependency]
        public IClienteBL clienteBL { get; set; }

        [Dependency]
        public IClienteJuridicoBL cliJuriBL { get; set; }

        [Dependency]
        public IClienteNaturalBL cliNatBL { get; set; }

        public FrmBuscarCliente()
        {
            InitializeComponent();
            this.BuildUp();
        }

        private void FrmBuscarCliente_Load(object sender, EventArgs e)
        {
            cargarDatos();
        }

        private void cargarDatos()
        {
            clienteJuridicoBindingSource.DataSource = cliJuriBL.GetAllFromClienteJuridico();
            clienteNaturalBindingSource.DataSource = cliNatBL.GetAllFromClienteNatural();

        }

        private void btnCrear_Click(object sender, EventArgs e)
        {
            FrmRegistrarCliente frm = new FrmRegistrarCliente();
            frm.Nuevo();
            frm.ShowDialog();
            cargarDatos();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var clienteNatural = clienteNaturalBindingSource.Current as ClienteNatural;

            if (clienteNatural != null)
            {
                FrmRegistrarCliente frm = new FrmRegistrarCliente();
                frm._clienteNatural = clienteNatural;
                frm.Editar();
                frm.ShowDialog();
                cargarDatos();
            }
            else
            {
                MessageBox.Show("Mensaje", "Seleccione un Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        //boton buscar cliente natural
        private void btnBuscar_Click(object sender, EventArgs e)
        {
            if (rbDatos.Checked == true)
            {
                clienteNaturalBindingSource.DataSource = cliNatBL.GetAllFromClienteNaturalByCriterio(txtDescripcionNatural.Text);
                if (clienteNaturalBindingSource.Count == 0)
                {
                    MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (rbDni.Checked == true)
            {
                clienteNaturalBindingSource.DataSource = cliNatBL.GetAllFromClienteNaturalByCriterio(txtDescripcionNatural.Text);
                if (clienteNaturalBindingSource.Count == 0)
                {
                    MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
        public Cliente CurrentCliente { get; set; }

        private void clienteDataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            CurrentCliente = clienteBindingSource.Current as Cliente;
        }

        private void clienteDataGridView_DoubleClick(object sender, EventArgs e)
        {
            this.Close();
        }


        //buscar cliente natural
        private void txtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode == Keys.Enter)
            {
                if (rbDatos.Checked == true)
                {
                    clienteNaturalBindingSource.DataSource = cliNatBL.GetAllFromClienteNaturalByCriterio(txtDescripcionNatural.Text);
                    if (clienteNaturalBindingSource.Count == 0)
                    {
                        MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else if (rbDni.Checked == true)
                {
                    clienteNaturalBindingSource.DataSource = cliNatBL.GetAllFromClienteNaturalByCriterio(txtDescripcionNatural.Text);
                    if (clienteNaturalBindingSource.Count == 0)
                    {
                        MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void btnBuscarJuridico_Click(object sender, EventArgs e)
        {
            if (rbRazSocial.Checked == true)
            {
                clienteJuridicoBindingSource.DataSource = cliJuriBL.GetAllFromClienteJuridicoByCriterio(txtDescripcionJuridico.Text);
                if (clienteJuridicoBindingSource.Count == 0)
                {
                    MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else if (rbRuc.Checked == true)
            {
                clienteJuridicoBindingSource.DataSource = cliJuriBL.GetAllFromClienteJuridicoByCriterio(txtDescripcionJuridico.Text);
                if (clienteJuridicoBindingSource.Count == 0)
                {
                    MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }

        private void txtDescripcionJuridico_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (rbRazSocial.Checked == true)
                {
                    clienteJuridicoBindingSource.DataSource = cliJuriBL.GetAllFromClienteJuridicoByCriterio(txtDescripcionJuridico.Text);
                    if (clienteJuridicoBindingSource.Count == 0)
                    {
                        MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                else if (rbRuc.Checked == true)
                {
                    clienteJuridicoBindingSource.DataSource = cliJuriBL.GetAllFromClienteJuridicoByCriterio(txtDescripcionJuridico.Text);
                    if (clienteJuridicoBindingSource.Count == 0)
                    {
                        MessageBox.Show("No se encuentra el Cliente", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
        }

        private void txtDescripcionNatural_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbDatos.Checked == true)
            {
                if (Char.IsLetter(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
            else if (rbDni.Checked == true)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void txtDescripcionJuridico_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (rbRuc.Checked == true)
            {
                if (Char.IsNumber(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsControl(e.KeyChar))
                {
                    e.Handled = false;
                }
                else if (Char.IsSeparator(e.KeyChar))
                {
                    e.Handled = false;
                }
                else
                {
                    e.Handled = true;
                }
            }
        }

        private void rbDatos_CheckedChanged(object sender, EventArgs e)
        {
            if (rbDatos.Checked == true)
            {
                txtDescripcionNatural.Text = "";
            }
            else if (rbDni.Checked == true)
            {
                txtDescripcionNatural.Text = "";
            }
        }

        private void rbRazSocial_CheckedChanged(object sender, EventArgs e)
        {
            if (rbRazSocial.Checked == true)
            {
                txtDescripcionJuridico.Text = "";
            }
            else if (rbRuc.Checked == true)
            {
                txtDescripcionJuridico.Text = "";
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            var clieneteJuridico = clienteJuridicoBindingSource.Current as ClienteJuridico;
            if (clieneteJuridico != null)
            {
                FrmRegistrarCliente frm = new FrmRegistrarCliente();
                frm._clienteJuridico = clieneteJuridico;
                frm.Editar();
                frm.ShowDialog();
                cargarDatos();
            }
            else
            {
                MessageBox.Show("Mensaje", "Seleccione un Item", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void clienteNaturalDataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                var currentClienteNatu = clienteNaturalBindingSource.Current as ClienteNatural;
                for (int count = 0; count < clienteNaturalDataGridView.Rows.Count; count++)
                {
                    if (currentClienteNatu != null)
                    {
                        Cliente cli = clienteNaturalDataGridView.Rows[count].Cells[4].Value as Cliente;

                        var u = (from c in cliNatBL.GetAllFromClienteNatural()
                                 where c.CliNaturalId.Equals(cli.Id)
                                 select c).SingleOrDefault();
                        u.Cliente = cli;

                        clienteNaturalDataGridView.Rows[count].Cells[5].Value = u.Cliente.Direccion.ToString();
                        clienteNaturalDataGridView.Rows[count].Cells[6].Value = u.Cliente.Telefono.ToString();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        private void clienteJuridicoDataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            try
            {
                var currentClienteJuri = clienteJuridicoBindingSource.Current as ClienteJuridico;
                for (int count = 0; count < clienteJuridicoDataGridView.Rows.Count; count++)
                {
                    if (currentClienteJuri != null)
                    {
                        Cliente cli = clienteJuridicoDataGridView.Rows[count].Cells[2].Value as Cliente;

                        var u = (from c in cliJuriBL.GetAllFromClienteJuridico()
                                 where c.IdCliJuridico.Equals(cli.Id)
                                 select c).SingleOrDefault();
                        u.Cliente = cli;

                        clienteJuridicoDataGridView.Rows[count].Cells[3].Value = u.Cliente.Direccion.ToString();
                        clienteJuridicoDataGridView.Rows[count].Cells[4].Value = u.Cliente.Telefono.ToString();
                    }
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Error");
            }
        }

        public ClienteNatural currentClienteNatural { get; set; }

        private void clienteNaturalDataGridView_DoubleClick(object sender, EventArgs e)
        {
            currentClienteNatural = clienteNaturalBindingSource.Current as ClienteNatural;
            this.Close();
        }

        public ClienteJuridico currentClienteJuridico { get; set; }

        private void clienteJuridicoDataGridView_DoubleClick(object sender, EventArgs e)
        {
            currentClienteJuridico = clienteJuridicoBindingSource.Current as ClienteJuridico;
            this.Close();
        }
      }
   }

